﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Manager.DBUtility;//Please add references
namespace Manager.DAL.Admin
{
    /// <summary>
    /// 数据访问类:M_UserDal
    /// </summary>
    public partial class M_UserDal
    {
        public M_UserDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int UserID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from M_User");
            strSql.Append(" where UserID=@UserID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)
			};
            parameters[0].Value = UserID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Manager.Model.Admin.M_User model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into M_User(");
            strSql.Append("UserName,UserNickName,UserPwd,UserImage,UserGender,UserEmail,UserTel,UserRegDate,UserIPAddr,UserAddrProvince,UserAddrCity,UserAddrDistrict,IsValid)");
            strSql.Append(" values (");
            strSql.Append("@UserName,@UserNickName,@UserPwd,@UserImage,@UserGender,@UserEmail,@UserTel,@UserRegDate,@UserIPAddr,@UserAddrProvince,@UserAddrCity,@UserAddrDistrict,@IsValid)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@UserNickName", SqlDbType.NVarChar,50),
					new SqlParameter("@UserPwd", SqlDbType.NVarChar,50),
					new SqlParameter("@UserImage", SqlDbType.NVarChar,200),
					new SqlParameter("@UserGender", SqlDbType.Bit,1),
					new SqlParameter("@UserEmail", SqlDbType.NVarChar,50),
					new SqlParameter("@UserTel", SqlDbType.NVarChar,50),
					new SqlParameter("@UserRegDate", SqlDbType.DateTime),
					new SqlParameter("@UserIPAddr", SqlDbType.NVarChar,50),
					new SqlParameter("@UserAddrProvince", SqlDbType.Int,4),
					new SqlParameter("@UserAddrCity", SqlDbType.Int,4),
					new SqlParameter("@UserAddrDistrict", SqlDbType.Int,4),
					new SqlParameter("@IsValid", SqlDbType.Bit,1)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.UserNickName;
            parameters[2].Value = model.UserPwd;
            parameters[3].Value = model.UserImage;
            parameters[4].Value = model.UserGender;
            parameters[5].Value = model.UserEmail;
            parameters[6].Value = model.UserTel;
            parameters[7].Value = model.UserRegDate;
            parameters[8].Value = model.UserIPAddr;
            parameters[9].Value = model.UserAddrProvince;
            parameters[10].Value = model.UserAddrCity;
            parameters[11].Value = model.UserAddrDistrict;
            parameters[12].Value = model.IsValid;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Manager.Model.Admin.M_User model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update M_User set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("UserNickName=@UserNickName,");
            strSql.Append("UserPwd=@UserPwd,");
            strSql.Append("UserImage=@UserImage,");
            strSql.Append("UserGender=@UserGender,");
            strSql.Append("UserEmail=@UserEmail,");
            strSql.Append("UserTel=@UserTel,");
            strSql.Append("UserRegDate=@UserRegDate,");
            strSql.Append("UserIPAddr=@UserIPAddr,");
            strSql.Append("UserAddrProvince=@UserAddrProvince,");
            strSql.Append("UserAddrCity=@UserAddrCity,");
            strSql.Append("UserAddrDistrict=@UserAddrDistrict,");
            strSql.Append("IsValid=@IsValid");
            strSql.Append(" where UserID=@UserID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@UserNickName", SqlDbType.NVarChar,50),
					new SqlParameter("@UserPwd", SqlDbType.NVarChar,50),
					new SqlParameter("@UserImage", SqlDbType.NVarChar,200),
					new SqlParameter("@UserGender", SqlDbType.Bit,1),
					new SqlParameter("@UserEmail", SqlDbType.NVarChar,50),
					new SqlParameter("@UserTel", SqlDbType.NVarChar,50),
					new SqlParameter("@UserRegDate", SqlDbType.DateTime),
					new SqlParameter("@UserIPAddr", SqlDbType.NVarChar,50),
					new SqlParameter("@UserAddrProvince", SqlDbType.Int,4),
					new SqlParameter("@UserAddrCity", SqlDbType.Int,4),
					new SqlParameter("@UserAddrDistrict", SqlDbType.Int,4),
					new SqlParameter("@IsValid", SqlDbType.Bit,1),
					new SqlParameter("@UserID", SqlDbType.Int,4)};
            parameters[0].Value = model.UserName;
            parameters[1].Value = model.UserNickName;
            parameters[2].Value = model.UserPwd;
            parameters[3].Value = model.UserImage;
            parameters[4].Value = model.UserGender;
            parameters[5].Value = model.UserEmail;
            parameters[6].Value = model.UserTel;
            parameters[7].Value = model.UserRegDate;
            parameters[8].Value = model.UserIPAddr;
            parameters[9].Value = model.UserAddrProvince;
            parameters[10].Value = model.UserAddrCity;
            parameters[11].Value = model.UserAddrDistrict;
            parameters[12].Value = model.IsValid;
            parameters[13].Value = model.UserID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int UserID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from M_User ");
            strSql.Append(" where UserID=@UserID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)
			};
            parameters[0].Value = UserID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string UserIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from M_User ");
            strSql.Append(" where UserID in (" + UserIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Manager.Model.Admin.M_User GetModel(int UserID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserID,UserName,UserNickName,UserPwd,UserImage,UserGender,UserEmail,UserTel,UserRegDate,UserIPAddr,UserAddrProvince,UserAddrCity,UserAddrDistrict,IsValid from M_User ");
            strSql.Append(" where UserID=@UserID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4)
			};
            parameters[0].Value = UserID;

            Manager.Model.Admin.M_User model = new Manager.Model.Admin.M_User();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["UserID"] != null && ds.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserName"] != null && ds.Tables[0].Rows[0]["UserName"].ToString() != "")
                {
                    model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserNickName"] != null && ds.Tables[0].Rows[0]["UserNickName"].ToString() != "")
                {
                    model.UserNickName = ds.Tables[0].Rows[0]["UserNickName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserPwd"] != null && ds.Tables[0].Rows[0]["UserPwd"].ToString() != "")
                {
                    model.UserPwd = ds.Tables[0].Rows[0]["UserPwd"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserImage"] != null && ds.Tables[0].Rows[0]["UserImage"].ToString() != "")
                {
                    model.UserImage = ds.Tables[0].Rows[0]["UserImage"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserGender"] != null && ds.Tables[0].Rows[0]["UserGender"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["UserGender"].ToString() == "1") || (ds.Tables[0].Rows[0]["UserGender"].ToString().ToLower() == "true"))
                    {
                        model.UserGender = true;
                    }
                    else
                    {
                        model.UserGender = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["UserEmail"] != null && ds.Tables[0].Rows[0]["UserEmail"].ToString() != "")
                {
                    model.UserEmail = ds.Tables[0].Rows[0]["UserEmail"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserTel"] != null && ds.Tables[0].Rows[0]["UserTel"].ToString() != "")
                {
                    model.UserTel = ds.Tables[0].Rows[0]["UserTel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserRegDate"] != null && ds.Tables[0].Rows[0]["UserRegDate"].ToString() != "")
                {
                    model.UserRegDate = DateTime.Parse(ds.Tables[0].Rows[0]["UserRegDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserIPAddr"] != null && ds.Tables[0].Rows[0]["UserIPAddr"].ToString() != "")
                {
                    model.UserIPAddr = ds.Tables[0].Rows[0]["UserIPAddr"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserAddrProvince"] != null && ds.Tables[0].Rows[0]["UserAddrProvince"].ToString() != "")
                {
                    model.UserAddrProvince = int.Parse(ds.Tables[0].Rows[0]["UserAddrProvince"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserAddrCity"] != null && ds.Tables[0].Rows[0]["UserAddrCity"].ToString() != "")
                {
                    model.UserAddrCity = int.Parse(ds.Tables[0].Rows[0]["UserAddrCity"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserAddrDistrict"] != null && ds.Tables[0].Rows[0]["UserAddrDistrict"].ToString() != "")
                {
                    model.UserAddrDistrict = int.Parse(ds.Tables[0].Rows[0]["UserAddrDistrict"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsValid"] != null && ds.Tables[0].Rows[0]["IsValid"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsValid"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsValid"].ToString().ToLower() == "true"))
                    {
                        model.IsValid = true;
                    }
                    else
                    {
                        model.IsValid = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Manager.Model.Admin.M_User GetModel(string username,string pwd)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserID,UserName,UserNickName,UserPwd,UserImage,UserGender,UserEmail,UserTel,UserRegDate,UserIPAddr,UserAddrProvince,UserAddrCity,UserAddrDistrict,IsValid from M_User ");
            strSql.Append(" where UserNickName=@UserNickName and UserPwd=@UserPwd");
            SqlParameter[] parameters = {
					new SqlParameter("@UserNickName", SqlDbType.NVarChar,50),
                    new SqlParameter("@UserPwd",SqlDbType.NVarChar,50)
			};
            parameters[0].Value = username;
            parameters[1].Value = pwd;

            Manager.Model.Admin.M_User model = new Manager.Model.Admin.M_User();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["UserID"] != null && ds.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserName"] != null && ds.Tables[0].Rows[0]["UserName"].ToString() != "")
                {
                    model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserNickName"] != null && ds.Tables[0].Rows[0]["UserNickName"].ToString() != "")
                {
                    model.UserNickName = ds.Tables[0].Rows[0]["UserNickName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserPwd"] != null && ds.Tables[0].Rows[0]["UserPwd"].ToString() != "")
                {
                    model.UserPwd = ds.Tables[0].Rows[0]["UserPwd"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserImage"] != null && ds.Tables[0].Rows[0]["UserImage"].ToString() != "")
                {
                    model.UserImage = ds.Tables[0].Rows[0]["UserImage"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserGender"] != null && ds.Tables[0].Rows[0]["UserGender"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["UserGender"].ToString() == "1") || (ds.Tables[0].Rows[0]["UserGender"].ToString().ToLower() == "true"))
                    {
                        model.UserGender = true;
                    }
                    else
                    {
                        model.UserGender = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["UserEmail"] != null && ds.Tables[0].Rows[0]["UserEmail"].ToString() != "")
                {
                    model.UserEmail = ds.Tables[0].Rows[0]["UserEmail"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserTel"] != null && ds.Tables[0].Rows[0]["UserTel"].ToString() != "")
                {
                    model.UserTel = ds.Tables[0].Rows[0]["UserTel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserRegDate"] != null && ds.Tables[0].Rows[0]["UserRegDate"].ToString() != "")
                {
                    model.UserRegDate = DateTime.Parse(ds.Tables[0].Rows[0]["UserRegDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserIPAddr"] != null && ds.Tables[0].Rows[0]["UserIPAddr"].ToString() != "")
                {
                    model.UserIPAddr = ds.Tables[0].Rows[0]["UserIPAddr"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserAddrProvince"] != null && ds.Tables[0].Rows[0]["UserAddrProvince"].ToString() != "")
                {
                    model.UserAddrProvince = int.Parse(ds.Tables[0].Rows[0]["UserAddrProvince"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserAddrCity"] != null && ds.Tables[0].Rows[0]["UserAddrCity"].ToString() != "")
                {
                    model.UserAddrCity = int.Parse(ds.Tables[0].Rows[0]["UserAddrCity"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserAddrDistrict"] != null && ds.Tables[0].Rows[0]["UserAddrDistrict"].ToString() != "")
                {
                    model.UserAddrDistrict = int.Parse(ds.Tables[0].Rows[0]["UserAddrDistrict"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsValid"] != null && ds.Tables[0].Rows[0]["IsValid"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsValid"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsValid"].ToString().ToLower() == "true"))
                    {
                        model.IsValid = true;
                    }
                    else
                    {
                        model.IsValid = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserID,UserName,UserNickName,UserPwd,UserImage,UserGender,UserEmail,UserTel,UserRegDate,UserIPAddr,UserAddrProvince,UserAddrCity,UserAddrDistrict,IsValid ");
            strSql.Append(" FROM M_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" UserID,UserName,UserNickName,UserPwd,UserImage,UserGender,UserEmail,UserTel,UserRegDate,UserIPAddr,UserAddrProvince,UserAddrCity,UserAddrDistrict,IsValid ");
            strSql.Append(" FROM M_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM M_User ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.UserID desc");
            }
            strSql.Append(")AS Row, T.*  from M_User T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "M_User";
            parameters[1].Value = "UserID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

